<?php
	function __construct(){
		require './model/orders_model.php';
		$_GET['a']();
	}
	//---------------------------------------
	function info(){
		if(isset($_SESSION['userInfo']) && $_SESSION['userInfo']){
			require './view/orders/info.php';
		}else{
			echo '您尚未登录!';
			$_SESSION['goOrders'] = true;
			echo "<meta http-equiv=refresh content='1,./index.php?c=user&a=login' />";	
		}
		
	}
	
	function addaddr(){
		$_SESSION['addaddr'] = true;
		echo "<meta http-equiv=refresh content='0,./index.php?c=orders&a=info' />";	
	}

	
	function doChangeAddr(){
		$_GET['tblname'] = 'shop_addr';
		$_GET['pk'] = 'uid';
		$_GET['id'] = $_SESSION['userInfo']['uid'];
		update();
		echo "<meta http-equiv=refresh content='0,./index.php?c=orders&a=info' />";	
	}
	
	function doAddAddr(){
		$_GET['tblname'] = 'shop_addr';
		$_GET['pk'] = 'uid';
		$_POST['uid'] = $_SESSION['userInfo']['uid'];
		insert();
		echo "<meta http-equiv=refresh content='0,./index.php?c=orders&a=info' />";	
	}
	
	function ydtz(){//原地跳转
		echo "<meta http-equiv=refresh content='0,./index.php?c=orders&a=info' />";	
	}
	
	function deladdr(){//删除地址
		$_GET['tblname'] = 'shop_addr';
		$_GET['pk'] = 'uid';
		$_GET['id'] = $_SESSION['userInfo']['uid'];
		delete();
		echo "<meta http-equiv=refresh content='0,./index.php?c=orders&a=info' />";	
	}
	function changeAdd(){//修改地址
		$_SESSION['addaddr'] = true;//显示地址表
		$_GET['tblname'] = 'shop_addr';
		$_GET['pk'] = 'uid';
		$uid = $_SESSION['userInfo']['uid'];
		$retAI = select(" WHERE uid=$uid ")[0];
		require './view/orders/info.php';
	}
	
	function mkorders(){//生成订单
		$time = time();
		$oid = date('YmdHis',$time).rand(1000,9999);
		$rmb = $_SESSION['endRmb'];
		$_POST['oid'] = $oid;//订单ID
		$_POST['ormb'] = $rmb;//金额
		$_POST['uid'] = $uid = $_SESSION['userInfo']['uid'];//下单人UID
		$_GET['tblname'] = 'shop_addr';
		$_GET['pk'] = 'uid';
		$addInfo = select(" WHERE uid=$uid")[0];
		$_POST['rec'] = $addInfo['name'];//收货人
		$_POST['addr'] = $addInfo['prov'].$addInfo['city'].$addInfo['dist'].$addInfo['addr'];//收货地址
		$_POST['tel'] = $addInfo['tel'];//联系电话
		$_POST['otime'] = $time;//下单时间
		
		$_GET['tblname'] = 'shop_orders';
		$_GET['pk'] = 'oid';
		insert();
		unset($_POST);
		//-------------------------------------------
		foreach($_SESSION['cart'] as $good){
			unset($_POST);
			$_GET['tblname'] = 'shop_detail';
			$_GET['pk'] = 'did';			
			$_POST['oid'] = $oid;//订单
			$_POST['gid'] = $good['gid'];//商品ID
			$_POST['gname'] = $good['gname'];//商品名称
			$_POST['price'] = $good['price'];//单价
			$_POST['buycnt'] = $good['num'];//购买数量
			$_POST['buyprice'] = $good['price']*$good['num'];//总价
			insert();
			unset($_POST);
			//----------------------------
			$_GET['tblname'] = 'shop_goods';
			$_GET['pk'] = 'gid';
			//echo 'ID'.$good['gid'];
			//echo '库存'.$_SESSION['cart'][$good['gid']]['cnt'];echo '<hr />';die;
			$_POST['cnt']=$_SESSION['cart'][$good['gid']]['cnt']-$good['num'];
			$_GET['id'] = $good['gid'];
			update();
			//----------------------------
		}
		unset($_SESSION['cart']);
		echo "<meta http-equiv=refresh content='0,./index.php?c=orders&a=showend&oid=$oid&rmb=$rmb' />";	
	}
	function showend(){
		require './view/orders/end.php';
	}